local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "authsvrhelper"
local configdao = require "configdao"
require "enum"

local globalvarcfg = nil
local  GetAuthAppidAndSecrect = {}

--[[
//请求注册
//请求获得appid和secrect
message GetAuthAppidAndSecrectReq {
	optional Version version = 1;
	optional int32 auth_type = 2;
}

message GetAuthAppidAndSecrectRes {
	optional int32 errcode = 1;    //错误原因 0表示成功
	optional string errcodedes = 2;//错误描述	
	optional int32 auth_type = 3;
	optional string appid = 4;
	optional string secrect = 5;	
}
]]
function  GetAuthAppidAndSecrect.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper.get_server()
	if request == nil or request.version == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "GetAuthAppidAndSecrectRes", responsemsg)
		server.tcpmng.close_socket(fd)
		return
	end

	if request.auth_type ~= EauthType.AUTH_TYPE_GUEST 
		and request.auth_type ~= EauthType.AUTH_TYPE_WECHAT
		and request.auth_type ~= EauthType.AUTH_TYPE_SELF then
		responsemsg.errcode = EErrCode.ERR_INVALID_AUTHTYPE
		responsemsg.errcodedes = "无效的账号类型！"
		msghelper:send_resmsgto_client(fd, "GetAuthAppidAndSecrectRes", responsemsg)
		server.tcpmng.close_socket(fd)
		return		
	end

	responsemsg.auth_type = request.auth_type

	if globalvarcfg == nil then
		globalvarcfg = configdao.get_business_conf(100, 1000, "globalvarcfg")
	end

	if globalvarcfg == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求，没有对应的配置数据！"
		msghelper:send_resmsgto_client(fd, "GetAuthAppidAndSecrectRes", responsemsg)
		server.tcpmng.close_socket(fd)
		return				
	end

	responsemsg.appid = globalvarcfg.wechat_app_id
	responsemsg.secrect = globalvarcfg.wechat_app_secrect
	msghelper:send_resmsgto_client(fd, "GetAuthAppidAndSecrectRes", responsemsg)
	server.tcpmng.close_socket(fd)		
end

return GetAuthAppidAndSecrect
